import axios from "axios"
import {SERVER_URI_NJ,CONSTANTS, } from "../constants"
	import {
		IGetDataTickers,
		IGetFundingRates,
		IGetSpreads,
		ISaveSpreads,
	} from "../types"

// DataTickers getting
export const getDataTickersAction = (objParams: IGetDataTickers) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/additionals/get-data-tickers/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_DATA_TICKERS_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/additionals/get-data-tickers/`,
					},
				})
				console.log(error)
			})
	}
}

// fundingRates getting
export const getFundingRatesAction = (objParams: IGetFundingRates) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/additionals/get-funding-rates/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_FUNDING_RATES_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/additionals/get-funding-rates/`,
					},
				})
				console.log(error)
			})
	}
}

// spreads getting
export const getSpreadsAction = (objParams: IGetSpreads) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/additionals/get-spreads/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_SPREADS_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/additionals/get-spreads/`,
					},
				})
				console.log(error)
			})
	}
}

// spreads saving
export const saveSpreadsAction = (objParams: ISaveSpreads) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/additionals/save-spreads/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "SAVE_SPREADS_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/additionals/save-spreads/`,
					},
				})
				console.log(error)
			})
	}
}